package com.nanfang51g3.ckz.ui.update.info;

import android.text.TextUtils;

import com.nanfang51g3.ckz.di.ApiModule;
import com.nanfang51g3.ckz.domain.HttpResultBean;
import com.nanfang51g3.ckz.domain.UserBean;
import com.nanfang51g3.ckz.exec.Api2Exception;
import com.nanfang51g3.ckz.http.HttpResultFunc;
import com.nanfang51g3.ckz.http.RestApi;
import com.nanfang51g3.ckz.http.SubscriberInterface;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.MD5Utils;
import com.nanfang51g3.ckz.utils.SPUtil;
import com.nanfang51g3.ckz.utils.ToolUtils;

import java.util.TreeMap;

import javax.inject.Inject;

import retrofit2.Retrofit;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/4/13.
 */
public class InfoUpPresenter implements InfoUpContract.InfoUpActionsListener {

    String message;
    private final RestApi _restApi;
    private final InfoUpContract.View mView;

    @Inject
    public InfoUpPresenter(RestApi restApi, InfoUpContract.View mView) {
        KLog.i("TAG", "进入了...AddFinancialInfoPresenter");
        /**
         * @see ApiModule#provideRestApi(Retrofit)
         */
        this._restApi = restApi;
        KLog.i("TAG", _restApi == null ? "进入了...is null" : "进入了...not null");
        /**
         * @see InfoUpPresenterModule#InfoUpPresenterModule(InfoUpContract.View)
         */
        this.mView = mView;
        KLog.i("TAG", mView == null ? "进入了...is null" : "进入了...not null");
    }

    @Override
    public void updateUserName(String userid, final String username) {
        if (TextUtils.isEmpty(username)) {
            mView.showMessage("昵称不能为空");
            return;
        }
        if (username.length() > 4) {
            mView.showMessage("请输入长度为4个字符名称");
            return;
        }
        if (!ToolUtils.isLetterDigitOrChinese(username)) {
            mView.showMessage("请输入正确的名称");
            return;
        }
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("userid", userid);
        treeMap.put("username", username);
        String sign = MD5Utils.getAccessToken(treeMap);
        KLog.i("Evd", treeMap.toString() + "-" + sign);
        Observable observable = _restApi.reuserinfoUserNmae(Integer.valueOf(userid), username, sign);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new InfoUpdateResultFunc<UserBean>())
                .subscribe(new SubscriberInterface<UserBean>() {
                    @Override
                    public void onCompleted() {
                        KLog.i("onCompleted");
                    }

                    @Override
                    public void _onError(String msg) {
                        KLog.i("onError " + msg);
                        /**
                         * @see  InfoUpActivity#showMessage(String)
                         */
                        mView.showMessage(msg);

                    }

                    @Override
                    public void onNext(UserBean o) {
                        KLog.i("onNext" + o.toString());
                        SPUtil.putAndApply(mView.getContext(), "username", o.getUsername());
                        /**
                         * @see  InfoUpActivity#showMessage(String)
                         */
                        mView.showMessage(message);
                        /**
                         * @see  InfoUpActivity#finalView(int, String)
                         */
                        mView.finalView(InfoUpActivity.TYPE_01, username);

                    }
                });
    }

    @Override
    public void updateEmail(String userid, final String email) {
        if (TextUtils.isEmpty(email)) {
            mView.showMessage("邮箱不能为空");
            return;
        }
        if (!ToolUtils.isValidEmail(email)) {
            mView.showMessage("请输入正确的邮箱");
            return;
        }
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("userid", userid);
        treeMap.put("email", email);
        String sign = MD5Utils.getAccessToken(treeMap);
        KLog.i("Evd", treeMap.toString() + "-" + sign);
        Observable observable = _restApi.reuserinfoEmail(Integer.valueOf(userid), email, sign);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new InfoUpdateResultFunc<UserBean>())
                .subscribe(new SubscriberInterface<UserBean>() {
                    @Override
                    public void onCompleted() {
                        KLog.i("onCompleted");
                    }

                    @Override
                    public void _onError(String msg) {
                        KLog.i("onError " + msg);
                        /**
                         * @see  InfoUpActivity#showMessage(String)
                         */
                        mView.showMessage(msg);

                    }

                    @Override
                    public void onNext(UserBean o) {
                        KLog.i("onNext" + o.toString());
                        SPUtil.putAndApply(mView.getContext(), "email", o.getEmail());
                        /**
                         * @see  InfoUpActivity#showMessage(String)
                         */
                        mView.showMessage(message);
                        /**
                         * @see  InfoUpActivity#finalView(int, String)
                         */
                        mView.finalView(InfoUpActivity.TYPE_02, email);
                    }
                });

    }

    @Override
    public void updateQQ(String userid, final String qq) {
        if (TextUtils.isEmpty(qq)) {
            mView.showMessage("QQ号不能为空");
            return;
        }
        if (qq.length() > 14) {
            mView.showMessage("请输入正确的QQ帐号");
            return;
        }
        if (qq.length() < 4 || qq.length() > 14) {
            mView.showMessage("请输入正确的QQ帐号");
            return;
        }
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("userid", userid);
        treeMap.put("qq", qq);
        String sign = MD5Utils.getAccessToken(treeMap);
        KLog.i("Evd", treeMap.toString() + "-" + sign);
        Observable observable = _restApi.reuserinfoQQ(Integer.valueOf(userid), qq, sign);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new InfoUpdateResultFunc<UserBean>())
                .subscribe(new SubscriberInterface<UserBean>() {
                    @Override
                    public void onCompleted() {
                        KLog.i("onCompleted");
                    }

                    @Override
                    public void _onError(String msg) {
                        KLog.i("onError " + msg);
                        /**
                         * @see  InfoUpActivity#showMessage(String)
                         */
                        mView.showMessage(msg);

                    }

                    @Override
                    public void onNext(UserBean o) {
                        KLog.i("onNext" + o.toString());
                        SPUtil.putAndApply(mView.getContext(), "qq", o.getQq());
                        /**
                         * @see  InfoUpActivity#showMessage(String)
                         */
                        mView.showMessage(message);
                        /**
                         * @see  InfoUpActivity#finalView(int, String)
                         */
                        mView.finalView(InfoUpActivity.TYPE_03, qq);
                    }
                });
    }


    class InfoUpdateResultFunc<T> extends HttpResultFunc<T> {
        @Override
        public T call(HttpResultBean<T> httpResult) {
            KLog.i("Evd", httpResult == null ? "1-" : "0-" + httpResult.getCode() + "-" + httpResult.getInfo() + "-" + httpResult.getData().toString());
            if (httpResult.getCode() == 2 ||
                    httpResult.getCode() == 3 ||
                    httpResult.getCode() == 4 ||
                    httpResult.getCode() == 5 ||
                    httpResult.getCode() == 6 ||
                    httpResult.getCode() == 7 ||
                    httpResult.getCode() == 8 ||
                    httpResult.getCode() == 9 ||
                    httpResult.getCode() == 10 ||
                    httpResult.getCode() == 11) {
                throw new Api2Exception(httpResult.getInfo());
            }
            message = httpResult.getInfo();
            return httpResult.getData();
        }
    }
}
